package com.FH.permission.sys_role_filegui;

import com.FH.organization.fileupload.FileUploadShowDTO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Stateless
public class SysRoleFileGuiDAOImpl extends AccessDB implements SysRoleFileGuiDAO {

    @Override
    public ResultDTO addSysRoleFileGui(SysRoleFileGuiPullDTO sysRoleFileGuiPullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        int resultls = 0;

        if(sysRoleFileGuiPullDTO.getFilegui_id().length > 0){

            String deleteSql = "delete from sys_role_filegui where sys_role_id = '" + sysRoleFileGuiPullDTO.getSys_role_id() + "' ";

            addReportingTo(deleteSql);  //给用户添加或更新时删除所有用户角色

            for(int i = 0; i< sysRoleFileGuiPullDTO.getFilegui_id().length; i++){

                if(null!= sysRoleFileGuiPullDTO.getFilegui_id()[i]&&!"".equals(sysRoleFileGuiPullDTO.getFilegui_id()[i])){

                    String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

                    String sql = "insert into sys_role_filegui (sys_role_filegui_id,sys_role_id,filegui_id) values " +
                            "('" + uuid + "'," + Integer.parseInt(sysRoleFileGuiPullDTO.getSys_role_id()) + ",'" + sysRoleFileGuiPullDTO.getFilegui_id()[i] + "')";
                    resultls = this.addReportingTo(sql);
                }
            }
        }

//        resultls = this.addReportingTo(sql);


        if (resultls != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("角色文件柜分配成功");
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("角色文件柜分配失败");
        }

        return resultDTO;
    }

    @Override
    public ResultDTO getFileGuiById(String member_id) {

        ResultDTO resultDTO = new ResultDTO();

        String sql = "SELECT DISTINCT f.filegui_id,fg.name FROM sys_role_filegui f JOIN sys_role_member m ON f.sys_role_id = m.sys_role_id " +
                    "JOIN filegui fg ON f.filegui_id = fg.filegui_id WHERE m.member_id = '" + member_id + "' ";

        List<String[]> results = new ArrayList<String[]>();

        List<FileUploadShowDTO> fileguiList = new ArrayList<FileUploadShowDTO>();

        results = this.getReportingTo(sql);

        for(int i=0;i<results.size();i++){
            FileUploadShowDTO fileUploadShowDTO = new FileUploadShowDTO();
            fileUploadShowDTO.setFilegui_id(results.get(i)[1]);
            fileUploadShowDTO.setName(results.get(i)[2]);

            fileguiList.add(fileUploadShowDTO);
        }

        if (results.size() > 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("文件柜获取成功");
            resultDTO.setValues(fileguiList);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("文件柜获取失败");
        }

        return resultDTO;
    }
}
